const hre = require("hardhat");

async function main() {
  console.log("Deploying BSCLaunchPad contract...");

  // PancakeSwap Router addresses
  const PANCAKE_ROUTER_MAINNET = "0x10ED43C718714eb63d5aA57B78B54704E256024E"; // BSC Mainnet
  const PANCAKE_ROUTER_TESTNET = "0xD99D1c33F9fC3444f8101754aBC46c52416550D1";  // BSC Testnet
  
  // Get network
  const networkName = hre.network.name;
  console.log("Deploying to network:", networkName);
  
  // Choose router based on network
  let pancakeRouter;
  if (networkName === "bsc") {
    pancakeRouter = PANCAKE_ROUTER_MAINNET;
  } else if (networkName === "bscTestnet") {
    pancakeRouter = PANCAKE_ROUTER_TESTNET;
  } else {
    // For hardhat/localhost, use testnet router address
    pancakeRouter = PANCAKE_ROUTER_TESTNET;
    console.log("Using testnet router address for local network");
  }
  
  console.log("PancakeSwap Router address:", pancakeRouter);
  
  // Get the deployer signer
  const [deployer] = await hre.ethers.getSigners();
  console.log("Deploying contracts with the account:", deployer.address);
  console.log("Account balance:", (await hre.ethers.provider.getBalance(deployer.address)).toString());
  
  // Get the contract factory
  const BSCLaunchPad = await hre.ethers.getContractFactory("BSCLaunchPad");
  
  // Deploy the contract
  console.log("Deploying BSCLaunchPad...");
  const launchPad = await BSCLaunchPad.connect(deployer).deploy(pancakeRouter);
  
  await launchPad.waitForDeployment();
  
  console.log("BSCLaunchPad deployed to:", await launchPad.getAddress());
  console.log("Transaction hash:", launchPad.deploymentTransaction().hash);
  
  // Wait for a few block confirmations
  if (networkName !== "hardhat") {
    console.log("Waiting for block confirmations...");
    await launchPad.deploymentTransaction().wait(5);
    console.log("Contract confirmed!");
    
    // Verify the contract on BSCScan if on mainnet/testnet
    if (networkName === "bsc" || networkName === "bscTestnet") {
      console.log("Verifying contract on BSCScan...");
      try {
        await hre.run("verify:verify", {
          address: await launchPad.getAddress(),
          constructorArguments: [pancakeRouter],
        });
        console.log("Contract verified successfully!");
      } catch (error) {
        console.log("Contract verification failed:", error.message);
      }
    }
  }
  
  // Display deployment information
  console.log("\n=== Deployment Summary ===");
  console.log("Network:", networkName);
  console.log("Contract Address:", await launchPad.getAddress());
  console.log("PancakeSwap Router:", pancakeRouter);
  console.log("Deployer:", deployer.address);
  
  console.log("\n=== Contract Configuration ===");
  console.log("Platform Fee:", "2% (200 basis points)");
  console.log("Min Target:", "1 BNB");
  console.log("Max Target:", "100 BNB");
  console.log("Launch Duration:", "7 days");
  console.log("Graduation Threshold:", "80% of target");
  console.log("Base Token Price:", "0.0001 BNB");
  console.log("Price Increment:", "0.00001 BNB per token sold");
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });